﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Util;
namespace UtilEditors
{
    public class Rigidbody2DEditers : InspectorCom<Rigidbody2D>
    {
        public override void Init(SubsidiaryWindow win, int windownID)
        {
            rect.width = 200;
            rect.height = 300;
            base.Init(win, windownID);
        }
        public Vector2 dir;
        bool isC = false;
        public override void OnIGUI()
        {
            Inpt("dir", ref dir);
            Toggle("", ref isC);
            if (Button("Add速度"))
            {
                component.byInvokeAll((r) =>
                {
                    r.velocity += dir;
                });
            }
            if (Button("Add力"))
            {
                component.byInvokeAll((r) =>
                {
                    r.AddForce(dir);
                });
            }
            if (isC)
            {
                component.byInvokeAll((r) =>
                {
                    r.AddForce(dir);
                });
            }
        }
    }
    public class RigidbodyEditers : InspectorCom<Rigidbody>
    {
        public override void Init(SubsidiaryWindow win, int windownID)
        {
            rect.width = 200;
            rect.height = 300;
            base.Init(win, windownID);
        }
        public Vector3 dir;

        public override void OnIGUI()
        {
            Inpt("dir", ref dir);
            if (Button("Add速度"))
            {
                component.byInvokeAll((r) =>
                {
                    r.velocity += dir;
                });
            }

        }
    }
    public class PolygonCollder2dEditers : InspectorCom<PolygonCollider2D>
    {
        int m;
        float minir;
        float maxr;
        float a;
        float size;
        int num;
        float move;
        public override void Init(SubsidiaryWindow win, int windownID)
        {
            base.Init(win, windownID);
            rect.width = 130;
            rect.height = 300;
        }
        public void Col2D去除精度(PolygonCollider2D pc)
        {
            Undo.RecordObject(pc, "PolygonCollider2D poinst  RemoveAccuracy");
            for (int i = 0; i < pc.pathCount; i++)
            {
                var poinst = pc.GetPath(i);
                for (int j = 0; j < poinst.Length; j++)
                {
                    var node = poinst[j];
                    node.x = node.x.toRounds(1);
                    node.y = node.y.toRounds(1);
                    poinst[j] = node;
                }
                pc.SetPath(i, poinst);
            }
        }
        public void 多边形缩放(PolygonCollider2D pc)
        {
            Undo.RecordObject(pc, "PolygonCollider2D poinst  RemoveAccuracy");
            var pos = pc.transform.position.toVector2();
            for (int i = 0; i < pc.pathCount; i++)
            {
                var poinst = pc.GetPath(i);
                for (int j = 0; j < poinst.Length; j++)
                {
                    var cpos = poinst[j];
                    poinst[j] = cpos * size;
                }
                pc.SetPath(i, poinst);
            }
        }
        public void 平移(PolygonCollider2D pc)
        {
            Undo.RecordObject(pc, "PolygonCollider2D poinst  RemoveAccuracy");
            for (int i = 0; i < pc.pathCount; i++)
            {
                var poinst = pc.GetPath(i);
                for (int j = 0; j < poinst.Length; j++)
                {
                    var cpos = poinst[j];
                    poinst[j] = cpos + inpt * move;
                }
                pc.SetPath(i, poinst);
            }
        }

        public void 齿轮(PolygonCollider2D pc)
        {
            Undo.RecordObject(pc, "PolygonCollider2D poinst  RemoveAccuracy");
            var pos = Vector2.zero;
            float a = 360 / num;
            int n = 0;
            List<Vector2> npos = new List<Vector2>();
            for (float i = 0; i < 360; i += a)
            {
                Quaternion q = Quaternion.Euler(0, 0, i);
                Vector2 dir = q * Vector2.right;

                if (n % 2 == 0)
                {
                    dir *= 1.2f;
                }
                var poinst = pos + dir;
                npos.Add(poinst);
                n++;
            }
            pc.SetPath(0, npos);
        }

        public void 扇形圆环(PolygonCollider2D pc)
        {
            Undo.RecordObject(pc, "PolygonCollider2D poinst  RemoveAccuracy");
            var ConeAll = new Mlist<Vector2>();
            var anglefov = a;
            var dist_min = minir;
            var dist_max = maxr;
            var angle_lookat = 0;
            var quality = m;//每10度一个面片
            var pos = Vector3.zero;
            float angle_fov = anglefov;

            float angle_start = angle_lookat/* - angle_fov*/;
            float angle_end = angle_lookat + angle_fov;
            float angle_delta = (angle_end - angle_start) / quality;

            float angle_curr = angle_start;
            List<Vector2> mini = new List<Vector2>();
            List<Vector2> max = new List<Vector2>();
            if (dist_min < 0.1f)
            {
                mini.Add(pos);
            }
            for (int i = 0; i < quality + 1; i++)
            {
                Vector3 sphere_curr = new Vector3();
                sphere_curr.y = Mathf.Cos(Mathf.Deg2Rad * angle_curr);
                sphere_curr.x = Mathf.Sin(Mathf.Deg2Rad * angle_curr);

                Vector3 pos_curr_min = pos + sphere_curr * dist_min;
                Vector3 pos_curr_max = pos + sphere_curr * dist_max;
                if (dist_min > 0.1f)
                    mini.Add(pos_curr_min);
                max.Add(pos_curr_max);

                angle_curr += angle_delta;
            }
            max.Reverse();
            ConeAll.AddRange(mini);
            ConeAll.AddRange(max);
            pc.SetPath(0, ConeAll.ToArray());
        }
        Vector2 inpt;
        public override void OnIGUI()
        {
            if (Button("Col2D去除精度"))
            {
                component.byInvokeAll(Col2D去除精度);
            }
            Inpt("缩放值", ref size);
            if (Button("多边形缩放"))
            {
                component.byInvokeAll(多边形缩放);
            }
            Inpt("平移", ref move);
            if (move != 0)
            {
                var x = 0f;
                var y = 0f;
                x = GUILayout.HorizontalScrollbar(x, 0, -1, 1);
                y = GUILayout.VerticalScrollbar(y, 0, 1, -1);
                if (x != 0 || y != 0)
                {
                    inpt = new Vector2(x, y);
                    inpt.Normalize();
                    component.byInvokeAll(平移);
                }
            }
            Inpt("锯齿数量", ref num);
            if (Button("齿轮"))
            {
                component.byInvokeAll(齿轮);
            }
            Inpt("内半径", ref minir);
            Inpt("外半径", ref maxr);
            Inpt("密度", ref m);
            Inpt("角度", ref a);
            if (Button("扇形圆环"))
            {
                component.byInvokeAll(扇形圆环);
            }
        }

    }
}